﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DATN.mds.ActionPlanManagement
{
    public partial class CreateNewAP : System.Web.UI.Page
    {
        SqlConnection cnn;
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                Load_data();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string name = TextBox1.Text;
                string time = TextBox2.Text;
                DateTime time1 = DateTime.Parse(time);
                time = time1.ToString("MM/dd/yyyy");
                string location = TextBox3.Text;
                string actual_ex = TextBox4.Text;
                string actual_re = TextBox5.Text;
                string Opp_Id="";
                string des = TextBox6.Text;
                Opp_Id = Session["Opp_Id"].ToString();
                //Name.Text = name;
                //Descrip.Text = description;
                //TimeRemind.Text = time;
                //Date.Text = date.ToString();


                SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                SqlCommand cmd = cnn.CreateCommand();

                string com1 = "Insert into ActionPlan (Name, Time, Location, ActualExpense, ActualRevenue, Opp_Id, Description)" +
                    "" + "values(N'" + name + "','" + time + "',N'" + location + "','"
                    + actual_ex + "','"
                    + actual_re + "','"
                    + Opp_Id + "',N'"
                    + des + "')";

                cnn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = com1;
                cmd.ExecuteNonQuery();
                               
                cnn.Close();
                Page.Response.Redirect("~/mds.ActionPlanManagement/IndexAP.aspx");


            }
        }

        protected void Load_data() 
        {
            try
            {
                cnn = new SqlConnection();
                cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn.Open();

                string sqlA = "Select * From Opportunities Where Opp_Id = " + Convert.ToInt32(Session["Opp_Id"].ToString());
                SqlDataAdapter da = new SqlDataAdapter(sqlA, cnn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Opportunities");

                decimal txt5 = decimal.Parse(ds.Tables["Opportunities"].Rows[0].ItemArray[6].ToString());
                decimal txt6 = decimal.Parse(ds.Tables["Opportunities"].Rows[0].ItemArray[7].ToString());

                Label1.Text = " (Số Vốn Dự Trù Của Cơ Hội Là: " + string.Format("{0:0,0}", txt5) + "VND)";
                Label2.Text = "(Số Lợi Nhuận Dự Trù Của Cơ Hội Là: " + string.Format("{0:0,0}", txt6) + "VND)";
                
                ds.Dispose();
                da.Dispose();
                cnn.Close();
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex);
            }
            finally
            {
                cnn.Close();
            }
        
        }
        protected void vali()
        {
            
        }

        
    }
}